//
//  InvestViewController.m
//  B-72K
//
//  Created by sos on 16/3/7.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "InvestViewController.h"
#import "InvestTableViewCell.h"
#import <MJRefresh.h>
#import "TopicPingViewController.h"
#import "QPNetWorkingTool.h"
#import "AllPrefixHeader.pch"
@interface InvestViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic , retain)UITableView *tableView;/***<tableView**/
@property (nonatomic ,  retain)NSMutableArray *dataArr;/***<用于接收网络请求的数据**/



@end

@implementation InvestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"话题";
//    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"15.jpg"]];
//    self.view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    [self.view setBackgroundColor:bgColor];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"15.jpg"]forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.view setBackgroundColor:bgColor];
    // tableView创建
    [self dataHandler];
    [self data];
    [self addNotificationCenter];
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        [self dataHandler];
        //刷新列表
        [self.tableView reloadData];
        //新版本是 MJ_header
        [self.tableView.mj_header endRefreshing];
        
    }];
    //设置刷新控件
    self.tableView.mj_header = header;
    //设置文字
    [header setTitle:@"使劲拉!" forState:MJRefreshStateIdle];
    [header setTitle:@"松手刷!" forState:MJRefreshStatePulling];
    [header setTitle:@"等！！！" forState:MJRefreshStateRefreshing];
    header.stateLabel.font = [UIFont systemFontOfSize:20];
    
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(requsetURL)];
    [self.tableView.mj_footer beginRefreshing];
    


}
- (void)statusBarPositionChange:(NSNotification *)noti
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        [self landscape];
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        
        [self landscape];
    } else if (orientation == UIInterfaceOrientationPortrait) {
        [self portrait];
    }
}

// 竖屏
- (void)portrait
{
    self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
// 横屏
- (void)landscape
{
   self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 
}
#pragma mark - 通知中心
- (void)addNotificationCenter
{
    // 监听屏幕旋转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarPositionChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}

// 网络请求方法
- (void)requsetURL
{
    static CGFloat n2 = 20;
    NSString *Str =[NSString stringWithFormat: @"http://c.m.163.com/newstopic/list/expert/0-%.0lf.html", n2];
    //    self.dataArr = [NSMutableArray array];
    [QPNetWorkingTool getNetWorkingTool:Str dic:nil success:^(id result) {
        self.dataArr = [InvestModel modelHanderWithArray:result[@"data"][@"expertList"]];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        
    }];
    n2 += 10;
}

- (void)dataHandler
{
    NSString *Str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    
    [QPNetWorkingTool getNetWorkingTool:Str dic:nil success:^(id result) {
        self.dataArr = [InvestModel modelHanderWithArray:result[@"data"][@"expertList"]];
    [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        
    }];
}
- (void)data
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView NightWithType:UIViewColorTypeNormal];
    self.tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerClass:[InvestTableViewCell class] forCellReuseIdentifier:@"investcell"];
    _tableView.rowHeight = 280 ;
    _tableView.backgroundColor = [UIColor clearColor];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InvestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"investcell" forIndexPath:indexPath];
    [cell NightWithType:UIViewColorTypeNormal];

    cell.invest = _dataArr[indexPath.row];
//    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicPingViewController *tpVC = [[TopicPingViewController alloc] init];
    InvestModel *m = _dataArr[indexPath.row];
    // 属性传值
    tpVC.str = m.expertId;
    
    [self.navigationController pushViewController:tpVC animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
